package be.redo.model.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * @author Frank Plain old Java class to represent a database table row.
 *         Represents geographical region in the domain model.
 */
@Entity
@NamedQueries({
		@NamedQuery(name = "findAantalRegions", query = "select count(r) from Region r"),
		@NamedQuery(name = "findAllRegions", query = "select r from Region r order by r.name"),
		@NamedQuery(name = "findRegionsByNameLike", query = "select r from Region r where r.name like( :name ) order by r.name"),
		@NamedQuery(name = "findRegionsByContinent", query = "select r from Region r where r.continent = :continent"),
		@NamedQuery(name = "findRegionsByFirstCharactersOfName", query = "select r from Region r where substring(r.name,1,length(:name)) = :name") })
public class Region implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6516244973033082567L;

	@NotNull
	@Size(min = 1, max = 200)
	private String name;

	@NotNull
	@ManyToOne
	@JoinColumn(name = "continent")
	private Continent continent;

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "region_id")
	private Long regionId;

	public Long getRegionId() {
		return this.regionId;
	}

	public void setRegionId(Long id) {
		this.regionId = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Continent getContinent() {
		return this.continent;
	}

	public void setContinent(Continent continent) {
		this.continent = continent;
	}

	/**
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((continent == null) ? 0 : continent.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/**
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Region other = (Region) obj;
		if (continent == null) {
			if (other.continent != null) {
				return false;
			}
		} else if (!continent.equals(other.continent)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

}
